<?php
	header('content-type:text/html;charset=utf-8');
	date_default_timezone_set("Asia/Chongqing");
	require('../config/config.php');
	require('../class/function.php');
	require('../class/autoload.php');
	session_start();



	//获取验证码
	if($_REQUEST['type'] == 'get_code'){
		require('../class/plugin/vcode.class.php');
		Code\VCode::GetCode();
		exit;
	}


	//生成二维码图片
	if($_REQUEST['type'] == 'erweima'){
		$url='http://www.bm123.top/nav.html?id=a1234';
		get_erweima($url);
	}



	//===== ### 请求token

	if($_REQUEST['type'] == 'token'){
		echo $token = md5(time()).md5(rand());
		$_SESSION[$_REQUEST['token_name']]= $token;
		exit;
	}





	//===== ### 验证token

	$token_arr = array(		// 跳过token验证的type项 添加到这里

	);
	if(!in_array($_REQUEST['type'],$token_arr)){
		if(!@$_REQUEST['remote_token']){	//验证本地token
			if(!@$_REQUEST['token'] || $_REQUEST['token'] != $_SESSION[$_REQUEST['type']]){
				$_REQUEST['token'] = md5(rand());
				exit('10001');
			}
			$_REQUEST['token'] = md5(rand());
		}else{		//验证远程token
			if(!CheckRemoteToken($_REQUEST['remote_token'])){
				exit('10002');	//远程token 验证失败
			}
		}
	}


	switch ($_REQUEST['type']) {

		default :
			echo 'default';
			break;

		//获取城市小区列表
		case 'get_county_area_list':
			$city_code = I('city_code','p');
			$openid = I('openid','p');
			$db_host = I('db_host','p');
			$db_name = I('db_name','p');
			if(!$city_code || !$db_host || !$db_name ||!$openid){
				exit('err1');
			}
			$pdo = MyPdoCounty::Init();
			$sql = 'select a.id,a.area_name,a.stat,a.pwd from area_list a left join user_area_list u on u.area_id = a.id where u.openid = ? and a.city_code = ?';
			$res = $pdo->dql($sql,array($openid,$city_code));
			dump($res);
			break;



		//获取我的小区列表
		case 'get_me_area_list':
			$city_code = I('city_code','p');
			$openid = I('openid','p');
			$db_host = I('db_host','p');
			$db_name = I('db_name','p');
			if(!$city_code || !$db_host || !$db_name ||!$openid){
				exit('err1');
			}
			$pdo = MyPdoCounty::Init();
			$sql = 'select a.id,a.area_name,a.stat,a.pwd from area_list a left join user_area_list u on u.area_id = a.id where u.openid = ? and a.city_code = ?';
			$res = $pdo->dql($sql,array($openid,$city_code));
			if($res){
				$res2 = json_encode($res);
				$tmp2 = array();
				foreach($res as $v){
					$tmp = array();
					$tmp[] = $v['id'];
					$tmp[] = $v['area_name'];
					$tmp[] = $v['stat'];
					$tmp[] = $v['pwd'];
					$tmp2[] = $tmp;
				}
				$res2 = json_encode($tmp2);
			}else{
				$res2 = '[]';
			}
			echo $res2;
			break;



	
		//创建小区
		case 'create_area':
			$vcode = I('vcode','p');
			$city_code = I('city_code','p');
			$area_name = I('area_name','p');
			$db_host = I('db_host','p');
			$db_name = I('db_name','p');
			$openid = I('openid','p');
			if(!$vcode || !$city_code || !$area_name || !$db_host || !$db_name || !$openid){
				echo 'err1';
			}
			if(!CheckCode($vcode)){
				//echo 'err2';
			}
			$pdo = MyPdoCounty::init();
			$sql = 'select stat from area_list where city_code = ? and area_name = ?';
			$res = $pdo->dql($sql,array($city_code,$area_name),1);
			if(!$res){
				$sql2 = "insert into area_list values(null,?,?,1,?,?)";
				$res2 = $pdo->dml($sql2,array($city_code,$area_name,$openid,rand(1000,9999)));
				if($res2){
					echo 'ok';
					//添加我的小区表
					$sql3 = 'insert into user_area_list values(?,?)';
					$res3 = $pdo->dml($sql3,array($openid,$res2));

					//添加到小区用户表
					$sql4 = 'insert into area_list_users values(?,?,1,1,1)';
					$res4 = $pdo->dml($sql4,array($res2,$openid));
					dump($res4);
				}
			}else{
				switch($res['stat']){
					case '1':case 1:echo 'err3';break;	//审核中
					case '2':case 2:echo 'err4';break; //已存在
					case '3':case 3:echo 'err5';break;	//被封禁
				}
			}
			exit;
			break;



		
	}









/*
	### 检测验证码是否正确
	参一：code  要比对的验证码
	返回：正确返回true,失败返回false
*/
function CheckCode($code){
	require('../class/plugin/vcode.class.php');
	if(Code\VCode::Check_Code($code)){
		return true;
	}else{
		return false;
	}
}


